{extend name="admin/layout" /}

{block name="content"}

<div class="layui-card layui-card-panel">
    <div class="layui-card-header">
        <form class="layui-form layui-row layui-col-space16">
            <div class="layui-col-md4">
                <div class="layui-input-wrap">
                    <input type="text" name="shop_id" value="" placeholder="店铺ID" class="layui-input" lay-affix="clear">
                </div>
            </div>
            <div class="layui-col-md4">
                <div class="layui-input-wrap">
                    <input type="text" name="name" placeholder="店铺名" lay-affix="clear" class="layui-input">
                </div>
            </div>

            <div class="layui-col-md4">
                <div class="layui-input-wrap">
                <button class="layui-btn" lay-submit lay-filter="demo-table-search">Search</button>
                </div>
            </div>
        </form>
    </div>
    
    <div class="layui-card-body">
        <table class="layui-hide" id="shop-list-search"></table>
    </div>
</div>

{/block}

{block name="js"}

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">新增</button>
    </div>
</script>

<script type="text/html" id="shop-operation">
    <div class="layui-clear-space">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除
        </a>
    </div>
</script>

<script type="text/html" id="switchTpl">
    <input type="checkbox" name="status" lay-skin="switch" lay-filter="statusSwitch" lay-text="ON|OFF" data-id="{{ d.id }}" {{ d.status == 'on' ? 'checked' : '' }}>
</script>

<script type="text/html" id="switchRepurchase">
    <input type="checkbox" name="is_repurchase" lay-skin="switch" lay-filter="statusRepurchase" lay-text="ON|OFF" data-id="{{ d.id }}" {{ d.is_repurchase == '1' ? 'checked' : '' }}>
</script>

<script>
    layui.use(['jquery','table'],function(){
        var table = layui.table;
        var form = layui.form;
        var $ = layui.$
        // 创建表格实例
        table.render({
            elem: '#shop-list-search',
            url: "{:url('/admin/shop/json')}", // 此处为静态模拟数据，实际使用时需换成真实接口
            toolbar: '#toolbarDemo',
            cols: [[
                {field:'id', title: 'ID', width:80, sort: true, fixed: true},
                {field:'name', title: '店铺名', width:120},
                {field:'host', title: '域名', width:300},
                {field:'website', title: '推广域名', width:300},
                {field:'is_repurchase', title: '是否加购', width:120,templet:'#switchRepurchase' },
                {field:'status', title: '状态', width:120,templet:'#switchTpl' },
                {field:'created_at', title: '创建时间',},
                {fixed: 'right', title:'操作', width: 134, minWidth: 125, toolbar: '#shop-operation'}
            ]],
            page: true,
            height: 500
        });
        // 搜索提交
        form.on('submit(demo-table-search)', function(data){
            var field = data.field; // 获得表单字段
            // 执行搜索重载
            table.reload('shop-list-search', {
                page: {
                    curr: 1 // 重新从第 1 页开始
                },
                where: field // 搜索的字段
            });

            return false; // 阻止默认 form 跳转
        });


        table.on('tool(shop-list-search)', function(obj){ // 双击 toolDouble
            var data = obj.data; // 获得当前行数据
            // console.log(obj)
            if(obj.event === 'edit'){
                let shop_edit_index = layer.open({
                    title: '编辑'+ data.name,
                    type: 2,
                    area: ['70%','85%'],
                    content: "/admin/shop/update/" + data.id,
                    end:()=>{
                        location.reload()
                    }
                });
                sessionStorage.setItem('shop_edit_index',shop_edit_index);

            } else if(obj.event === 'del'){
                layer.confirm("确认删除吗?",{icon: 3,title: '警告'},function (index){
                let url = `/admin/shop/del/${data.id}`
                  $.ajax({
                      url: url,
                      type: 'delete',
                      dataType: 'json',
                      success:function (data){
                        if(data.code == 200){
                            success('删除成功',function (){
                                location.reload();
                            })
                        }else{
                           error(data.msg)
                        }
                      }
                  })

                })
            }
        });

        // 工具栏事件
        table.on('toolbar(shop-list-search)', function(obj){
            var id = obj.config.id;
            switch(obj.event){
                case 'add':
                    var layer_index = layer.open({
                        title: '新增店铺',
                        type: 2,
                        area: ['60%','80%'],
                        content:"{:url('/admin/shop/add')}",
                        end: function (){
                            location.reload()
                        }
                    });
                    sessionStorage.setItem("shop_list_index",layer_index)
                    break;
            };
        });

        // 监听开关
        form.on('switch(statusSwitch)', function(data){
            var id = data.elem.getAttribute('data-id');
            var checked = data.elem.checked;
            $.ajax({
                url: `/admin/shop/status/${id}`,
                dataType: 'json',
                type: 'post',
                data:{status: checked ? 1 : 0},
                success:(data)=>{
                    if(data.code == 200){
                        success("修改成功",function (){
                            location.reload()
                        })
                    }else{
                        error(data.msg)
                    }
                }
            });
        });
        //监听加购 switchRepurchase
        form.on('switch(statusRepurchase)', function(data){
            var id = data.elem.getAttribute('data-id');
            var checked = data.elem.checked;
            $.ajax({
                url: `/admin/shop/repurchase/${id}`,
                dataType: 'json',
                type: 'post',
                data:{is_repurchase: checked ? 1 : 0},
                success:(data)=>{
                    if(data.code == 200){
                        success("修改成功",function (){
                            location.reload()
                        })
                    }else{
                        error(data.msg)
                    }
                }
            });
        });

    });
</script>

{/block}


